package listbyorder.access101_200.test107;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/7 10:24
 */
public class Solution1 {

    // 二叉树的层次遍历
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        dfs(res, 0, root);
        return res;
    }

    private void dfs(List<List<Integer>> res, int level, TreeNode root) {
        if (root == null) return;
        if (level == res.size()) {
            res.add(0, new ArrayList<Integer>());
        }
        res.get(res.size() - level - 1).add(root.val);
        dfs(res, level + 1, root.left);
        dfs(res, level + 1, root.right);
    }
}
